﻿#include "MeasureView/MeasureCircle3P.h"
#include "MeasureView/UnitConvert.h"
#include "MeasureView/MeasureDataSet.h"
#include <QtMath>
namespace Qly {

MeasureCircle3P::MeasureCircle3P(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_measureType = Circle3P;
    m_results_pix.resize(5);
    m_results_um.resize(5);
    m_keyPoints_pix.resize(4); //第四个用来存圆心坐标
    m_keyPoints_um.resize(4);
    m_name = u8"三点圆";
    m_textTemplate = parent->m_textTemplate_MeasureCircle3P;
}

int MeasureCircle3P::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix[0] = point_pix;
        m_keyPoints_um[0] = point_um;
        m_measureState = DS_STATE1;
        return m_measureState;
    }
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        m_measureState = DS_STATE2;
        return m_measureState;
    }
    if(m_measureState == DS_STATE2)
    {
        m_keyPoints_pix[2] = point_pix;
        m_keyPoints_um[2] = point_um;
        compute();
        if(m_results_pix[2] == 0)
        {
            m_measureState = DS_ERROR;
            return m_measureState;
        }
        else
        {
            updateText(true);

            if(m_textTemplate.contains("@radius"))
            {
                m_name += u8":半径";
            }
            else if(m_textTemplate.contains("@diameter"))
            {
                m_name += u8":直径";
            }
            else if(m_textTemplate.contains("@area"))
            {
                m_name += u8":面积";
            }
            m_measureState = DS_FINISH;
            return m_measureState;
        }
    }
    return m_measureState;
}

void MeasureCircle3P::compute()
{
    double x1 = m_keyPoints_pix[0].x();
    double y1 = m_keyPoints_pix[0].y();
    double x2 = m_keyPoints_pix[1].x();
    double y2 = m_keyPoints_pix[1].y();
    double x3 = m_keyPoints_pix[2].x();
    double y3 = m_keyPoints_pix[2].y();

    double a, b, c, d, e, f, g, x, y;
    a = 2 * (x2 - x1);
    b = 2 * (y2 - y1);
    c = x2 * x2 + y2 * y2 - x1 * x1 - y1 * y1;
    d = 2 * (x3 - x2);
    e = 2 * (y3 - y2);
    f = x3 * x3 + y3 * y3 - x2 * x2 - y2 * y2;
    g = b * d - e * a;
    if(fabs(g) < 1e-5)
    {
        m_results_pix[0] = 0;
        m_results_pix[1] = 0;
        m_results_pix[2] = 0;

        m_results_um[0] = 0;
        m_results_um[1] = 0;
        m_results_um[2] = 0;
        return ;
    }
    x = (b * f - e * c) / g;
    y = (d * c - a * f) / g;

    m_results_pix[0] = x;
    m_results_pix[1] = y;
    m_results_pix[2] = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
    m_results_pix[3] = m_results_pix[2] * 2;
    m_results_pix[4] = M_PI * m_results_pix[2] * m_results_pix[2]; // 面积

    double ps = m_pParent->m_pixSize_um;
    m_results_um[0] = ps * m_results_pix[0];
    m_results_um[1] = ps * m_results_pix[1];
    m_results_um[2] = ps * m_results_pix[2];
    m_results_um[3] = m_results_um[2] * 2;
    m_results_um[4] = ps * ps * m_results_pix[4];

    m_keyPoints_um[3] = QPointF(m_results_um[0], m_results_um[1]);
    m_keyPoints_pix[3] = QPointF(x, y);
}

int MeasureCircle3P::mouseMove(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE2)
    {
        m_keyPoints_pix[2] = point_pix;
        m_keyPoints_um[2] = point_um;
        compute();
        updateText(true);
        return 3;
    }
    return 0;
}


//int MeasureCircle3P::setLens(int oldLens, int newLens, int method)
//{
//    if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        return 0;
//    }
//    LensManager & lens = LensManager::getInstance();
//    if(method == 0)//只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//    }
//    else
//    {
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//    }
//    compute();
//    updateText();
//    return 1;
//}

void MeasureCircle3P::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE )
    {
        painter.save();
        painter.setPen(m_pen);
        int x, y, width, height;
        x = m_results_pix[0] - m_results_pix[2];
        y = m_results_pix[1] - m_results_pix[2];
        width = m_results_pix[3];
        height = m_results_pix[3];
        painter.drawArc ( x, y, width, height, 0, 16 * 360 );
        if( m_measureState == DS_STATE1 )
        {
            //painter.drawPoint(m_keyPoints_pix[0]);
            drawCross(painter, m_keyPoints_pix[0]);
        }
        else if( m_measureState == DS_STATE2  || m_measureState == DS_FINISH)
        {
            drawCross(painter, m_keyPoints_pix[0]);
            drawCross(painter, m_keyPoints_pix[1]);
            drawCross(painter, m_keyPoints_pix[2]);
//            QPointF points[3];
//            points[0] = m_keyPoints_pix[0];
//            points[1] = m_keyPoints_pix[1];
//            points[2] = m_keyPoints_pix[2];
//            painter.drawPoints(points, 3);

            drawCross(painter, QPointF(m_results_pix[0], m_results_pix[1]));
//            painter.drawLine(m_results_pix[0] - 5, m_results_pix[1], m_results_pix[0] + 5, m_results_pix[1]);
//            painter.drawLine(m_results_pix[0], m_results_pix[1] - 5, m_results_pix[0], m_results_pix[1] + 5);

            m_textBlock.draw(painter);
        }
        if(m_textMoving)
        {
            QPen pen = m_pen;
            pen.setStyle(Qt::DashDotLine);
            painter.setPen(pen);
            QPointF p = m_textBlock.anchorPoint();
            painter.drawLine(p, m_keyPoints_pix[3]);
            painter.setPen(m_pen);
        }
        painter.restore();
    }
}

void MeasureCircle3P::updateText(bool updatePosition )
{
    double dia = m_results_um[3];
    double radius = m_results_um[2];

    QString text = m_textTemplate;
    m_pParent->m_unitConvert_MeasureCircle3P.replaceLength(text, dia, "@diameter", "@unit");
    m_pParent->m_unitConvert_MeasureCircle3P.replaceLength(text, radius, "@radius", "@unit");
    m_pParent->m_unitConvert_MeasureCircle3P.replaceArea(text, m_results_um[4], "@area", "@areaUnit");

    QString str3 = QString::number(m_id);
    text.replace("@id", str3, Qt::CaseSensitive);

    m_textBlock.setText(text);
    if(updatePosition)
    {
        QPointF p_pix = QPointF(m_results_pix[0], m_results_pix[1]);
        QPointF p_um = QPointF(m_results_um[0], m_results_um[1]);
        m_textBlock.moveTo(p_pix, p_um);
    }
}
}
